package com.zj.eswrapper.wrapper;

import com.zj.eswrapper.interfaces.IGetter;
import com.zj.eswrapper.interfaces.field.ILambdaField;
import com.zj.eswrapper.interfaces.impl.AbstractEsSearchWrapper;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author arthur_zhou
 * @date 2023/12/28 18:31
 */
public class EsLambdaQueryWrapper<DO>
        extends AbstractEsLastQueryWrapper<IGetter<DO>, EsLambdaQueryWrapper<DO>, EsLambdaSearchWrapper<DO>, EsLambdaAggWrapper<DO>>
        implements ILambdaField<DO, EsQueryWrapper<DO>> {

    public EsLambdaQueryWrapper() {
        this(true);
    }

    /**
     * @param check 是否校验val的值。true时，val不能为空。关联的EsQueryWrapper全局公用
     */
    public EsLambdaQueryWrapper(boolean check) {
        this(new AtomicBoolean(check));
    }

    protected EsLambdaQueryWrapper(AtomicBoolean check) {
        super(check);
    }

    protected EsLambdaQueryWrapper(AbstractEsLastQueryWrapper<?, ?, ?, ?> queryWrapper) {
        super(queryWrapper);
    }

    @Override
    protected EsLambdaQueryWrapper<DO> createWrapper() {
        return new EsLambdaQueryWrapper<>(this.check);
    }

    @Override
    protected EsLambdaAggWrapper<DO> createAggWrapper() {
        return new EsLambdaAggWrapper<>();
    }

    @Override
    protected EsLambdaSearchWrapper<DO> createSearchWrapper(AbstractEsSearchWrapper<?, ?, SearchSourceBuilder> searchWrapper) {
        if (Objects.nonNull(searchWrapper)) {
            return new EsLambdaSearchWrapper<>(searchWrapper);
        }
        return new EsLambdaSearchWrapper<>(searchBuilder);
    }

    /**
     * 转换成普通条件拼接器
     *
     * @return EsQueryWrapper
     */
    @Override
    public EsQueryWrapper<DO> simple() {
        return new EsQueryWrapper<>(this);
    }
}
